﻿using OurMemory.Model.EF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OurMemory
{
    internal class ProblemSolvingServices
    {
        /// <summary>
        /// 获得所有数据
        /// </summary>
        /// <returns></returns>
        public List<ProblemSolving> GetAll()
        {
            var db = new OurMemoryEntities();
            return db.ProblemSolving.ToList();
        }
        /// <summary>
        /// 新建记录
        /// </summary>
        /// <param name="model"></param>

        public void Create(ProblemSolvingCreateModel model)
        {
            var db = new OurMemoryEntities();
            var newItem = new ProblemSolving()
            {
                Id = Guid.NewGuid(),
                Recorder = model.Recorder,
                Problem = model.Problem,
                Date = model.Date,
                Urgency = model.Urgency,
            };
            db.ProblemSolving.Add(newItem);
            db.SaveChanges();
        }
        /// <summary>
        /// 根据Id，获得指定记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ProblemSolving GetById(Guid id)
        {
            var db = new OurMemoryEntities();
            return db.ProblemSolving.Find(id);
        }

        /// <summary>
        /// 修改记录
        /// </summary>
        /// <param name="model"></param>
        public void Edit(ProblemSolvingEditModel model)
        {
            var db = new OurMemoryEntities();
            var item = db.ProblemSolving.Find(model.Id);
            if (item != null)
            {
                item.Recorder = model.Recorder;
                item.Problem = model.Problem;
                item.Date = model.Date;
                item.Urgency = model.Urgency;
            }
            db.SaveChanges();
        }
        /// <summary>
        /// 删除记录
        /// </summary>
        /// <param name="id"></param>
        public void Delete(Guid id)
        {
            var db = new OurMemoryEntities();
            var item = db.ProblemSolving.Find(id);
            if (item != null)
            {
                db.ProblemSolving.Remove(item);
                db.SaveChanges();
            }
        }

    }

}
